package com.poisonh.unifiedauth.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.model.InfoCenterModel;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息中心Adapter
 * Created by PoisonH on 2016/7/11.
 */
public class InfoCenterAdapter extends RecyclerView.Adapter<InfoCenterAdapter.MyViewHolder>
{
    private Context mContext;
    private List<InfoCenterModel> mList;

    public InfoCenterAdapter(Context context)
    {
        this.mContext = context.getApplicationContext();
        mList = new ArrayList<>();
    }


    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @SuppressLint("InflateParams")
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_item_infocenter, null));
    }

    //绑定数据到视图上
    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position)
    {
        holder.mTvTitle.setText(mList.get(position).getTITLE());
        holder.mTvTime.setText(mList.get(position).getTIME());
        holder.mTvDesc.setText(mList.get(position).getDESCRIBE());
        if (mList.get(position).getIMGURL() != null)
        {
            holder.mIvPic.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(ConstantUitls.REQUEDT_IMG_URL + mList.get(position).getIMGURL()).into(holder.mIvPic);
        } else
        {
            holder.mIvPic.setVisibility(View.GONE);
        }

        /**
         * 分别设置点击事件的回调
         */
        if (mOnItemClickLitener != null)
        {
            holder.mArlLayout.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition() - 1;
                    mOnItemClickLitener.onItemClick(v, pos);
                }
            });
        }

    }

    @Override
    public int getItemCount()
    {
        return mList.size();
    }

    protected class MyViewHolder extends RecyclerView.ViewHolder
    {
        private TextView mTvTitle;
        private TextView mTvTime;
        private TextView mTvDesc;
        private ImageView mIvPic;
        private AutoRelativeLayout mArlLayout;

        protected MyViewHolder(View view)
        {
            super(view);
            mTvTitle = (TextView) view.findViewById(R.id.tv_item_infocenter_tile);
            mTvTime = (TextView) view.findViewById(R.id.tv_item_infocenter_time);
            mTvDesc = (TextView) view.findViewById(R.id.tv_item_infocenter_desc);
            mIvPic = (ImageView) view.findViewById(R.id.iv_item_infocenter_pic);
            mArlLayout = (AutoRelativeLayout) view.findViewById(R.id.arl_layout);
        }
    }

    public void setData(List<InfoCenterModel> list)
    {
        this.mList.addAll(mList.size(), list);
        this.notifyDataSetChanged();
    }

    //更新数据集
    public void updateData(List<InfoCenterModel> list)
    {
        if (mList.size() > 0)
        {
            mList.clear();
        }
        this.mList.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 接口回调
     */
    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
    }

    public List<InfoCenterModel> getList()
    {
        return mList;
    }
}
